﻿/*优惠码后台验证 
 *Last edited by cyh
 *2013/11/14
 * */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;

public partial class discount_verify : System.Web.UI.Page
{
    string user_name;
    string discount_name;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] == null)
        {
            Response.Redirect("error_log", true);
        }
        user_name = "null";
        discount_name = "null";
        if (!IsPostBack)
        {
            label_info.Text = "";
        }
    }

    protected void button1_Click(object sender, EventArgs e)
    {
        if (textbox1.Text != null && textbox1.Text.Trim().Equals("") == false)
        {
            //先获取输入的优惠码，查询用户信息和优惠活动信息，返回给页面 
            string sncode = textbox1.Text;
            SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc.Open();
            DateTime now = DateTime.Now;
            string start_now = now.Year + "-" + now.Month + "-" + now.Day + " " + now.Hour + ":" + now.Minute + ":" + now.Second;
            string end_now = start_now;
            SqlCommand sncode_check = sc.CreateCommand();
            sncode_check.CommandText = "select INFO_DISCOUNT.DISCOUNT_TITLE,INFO_IND.IND_NAME from INFO_IND,INFO_DISCOUNT,DISCOUNT_ATTEND where INFO_DISCOUNT.DISCOUNT_ID=DISCOUNT_ATTEND.DISCOUNT_ID and INFO_IND.IND_ID=DISCOUNT_ATTEND.IND_ID and INFO_DISCOUNT.DISCOUNT_START<convert(smalldatetime,@start) and INFO_DISCOUNT.DISCOUNT_END>convert(smalldatetime,@end) and INFO_DISCOUNT.ENTER_ID=@eid1 and DISCOUNT_ATTEND.SN_CODE=@code and DISCOUNT_ATTEND.STATUS=0";
            /*1.确保优惠活动现在有效
             *2.确保优惠活动未被使用过
             *3.确保优惠活动属于该企业用户
             * */
            sncode_check.Parameters.AddWithValue("@start", start_now);
            sncode_check.Parameters.AddWithValue("@end", end_now);
            sncode_check.Parameters.AddWithValue("@eid1", Session["ENTERID"].ToString());
            sncode_check.Parameters.AddWithValue("@code", sncode);
            try
            {
                SqlDataReader r = sncode_check.ExecuteReader();
                while (r.Read())
                {
                    user_name = r.GetString(r.GetOrdinal("IND_NAME"));
                    discount_name = r.GetString(r.GetOrdinal("DISCOUNT_TITLE"));
                }
            }
            catch (Exception ex)
            {
                label_info.Text = "查询中可能有错误。" + ex.Message;
                return;
            }
            if (user_name != null && discount_name != null && user_name.Trim().Equals("") == false && discount_name.Trim().Equals("") == false && user_name.Equals("null") == false && discount_name.Equals("null") == false)
            {
                label_info.Text = "您所输入的SN码对应的用户名为" + user_name + "； 对应的优惠活动为" + discount_name;
                label_info1.Text = user_name;
                label_info2.Text = discount_name;
                /*两个label用来传递用户名和优惠活动名
                 * */
            }
            else
                label_info.Text = "您所查询的记录不存在";
            return;
        }
        else
        {
            Page.RegisterStartupScript("ss", "<script>alert('请输入有效的SN码！')</script>");
            return;
        }

    }
    protected void button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("sale_input.aspx?dname=" + label_info2.Text + "&iid=" + label_info1.Text);
        //无参数时默认为Null
    }
}
